﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using BobDotNet.Classes;
using BobDotNet.Classes.Dao;

namespace BobDotNet.Forms.Configuration
{
    public partial class EtatDevisForm : Form
    {
        public EtatDevisForm()
        {
            InitializeComponent();
            InitialiseGrid();
        }

        public void FocusOn(Int32 Id)
        {
            int i = MainGrid.Rows.Count;
            for (int j = 0; j < i; j++)
            {
                Int32 temp = (Int32)MainGrid.Rows[j].Cells[0].Value;
                if (temp.Equals(Id))
                {
                    MainGrid.CurrentCell = MainGrid[1, j];
                }
            }
        }

        public void InitialiseGrid()
        {
            // Mise à zéro du MainGrid
            this.MainGrid.Rows.Clear();
            this.MainGrid.Columns.Clear();
            // Creation des colonnes
            foreach (string s in EtatDevis.ObtenirEntetes())
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                col.HeaderText = s;
                col.Name = s.Replace(" ", String.Empty);
                col.ReadOnly = true;

                this.MainGrid.Columns.AddRange(new DataGridViewColumn[] { col });
            }
            // Chargements des lignes
            foreach (EtatDevis ed in DaoEtatDevis.ChargeTout())
            {
                MainGrid.Rows.Add(ed.ObtenirValeurs().ToArray());
            }
            // Positionnement sur la premiere ligne
            if (this.MainGrid.Rows.Count > 0)
            {
                this.MainGrid.CurrentCell = this.MainGrid[0, 0];
            }           
        }

        private void ajouter_Click(object sender, EventArgs e)
        {
            tsEtat.Text = "Création";
            supprimer.Enabled = false;
            ajouter.Enabled = false;
            txtLibelle.Text = String.Empty;
        }

        private void supprimer_Click(object sender, EventArgs e)
        {
            if (tsEtat.Text.Equals("Modification"))
            {
                if (MessageBox.Show("Etes-vous sur de supprimer cet Etat ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question).Equals(DialogResult.Yes))
                {
                    DaoEtatDevis.Supprimer((Int32)MainGrid.SelectedRows[0].Cells[0].Value);
                    DataGridViewRow row = MainGrid.SelectedRows[0];
                    MainGrid.Rows.Remove(row);
                }
            }
        }

        private void Valider_Click(object sender, EventArgs e)
        {
            if (!txtLibelle.Text.Trim().Equals(String.Empty))
            {
                EtatDevis ed = new EtatDevis();
                if (tsEtat.Text.Equals("Modification"))
                {
                    ed = DaoEtatDevis.Charge((Int32)this.MainGrid.SelectedRows[0].Cells["Identifiant"].Value);
                    ed = this.ChargeObjetAvecForm(ed);
                    DaoEtatDevis.MettreAJour(ed);
                    this.MainGrid.SelectedRows[0].Cells["Libelle"].Value = ed.Libelle;
                    this.MainGrid.SelectedRows[0].Cells["DateModification"].Value = ed.DateModification;
                }
                else
                {
                    ed = this.ChargeObjetAvecForm(ed);
                    ed.DateCreation = DateTime.Now;
                    DaoEtatDevis.Creer(ed);
                    this.MainGrid.Rows.Add(ed.ObtenirValeurs().ToArray());
                    this.MainGrid.CurrentCell = this.MainGrid[0, this.MainGrid.Rows.Count-1];
                }

            }
            else
            {
                MessageBox.Show("Vous devez préciser un Libellé.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ChargeFormAvecLigne(DataGridViewRow r)
        {
            this.txtLibelle.Text = r.Cells["Libelle"].Value.ToString();
        }

        private EtatDevis ChargeObjetAvecForm(EtatDevis ed)
        {
            ed.Libelle = this.txtLibelle.Text;
            ed.DateModification = DateTime.Now;
            return ed;
        }

        private void MainGrid_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (MainGrid.SelectedRows.Count > 0)
            {
                if (MainGrid.Rows.Count > 0)
                {
                    tsEtat.Text = "Modification";
                    supprimer.Enabled = true;
                    ajouter.Enabled = true;
                    this.ChargeFormAvecLigne(this.MainGrid.SelectedRows[0]);
                }
            }
        }
    }
}
